from flask import (render_template)
from app.spider.luoqiu import *
from . import home
from app.models.models import *
from flask import request

@home.route('/', methods=['GET','POST'])
def index():
    url = 'http://www.luoqiu.com/read/3713/index.html'

    cookie = FictionCooke()

    cookie.cookie_value = 'Hm_lvt_7d206fb19764ee3cea0cf0a719cf78b8=1515211354; Hm_lpvt_7d206fb19764ee3cea0cf0a719cf78b8=1515215017; UM_distinctid=160b14ab3cd64b-088b2225e7be8-e323462-1fa400-160b14ab3ceb8d; visid_incap_1454401=aNtdhD3qSGCI0aMQfB7xJAUPSloAAAAAQUIPAAAAAACClNqa2hhNCPTUqDHIzPgd; incap_ses_199_1454401=C/0yDab7kV2ttpvMEv7CAuJrUFoAAAAAvjKYEdCQWTrmgGaV0iGi9Q==; CNZZDATA1257366169=1353596363-1514802362-null%7C1515218164'
    cookies = cookie.resolve()

    spider = LuoQiuSpider()
    spider.set_cookies(cookies)
    list = spider.resolve_catalog(url)

    data = {'title':'hello world','list':list}
    return render_template('home/index.html', **data)


@home.route('/read',methods=['GET','POST'])
def detail():
    cookie = FictionCooke()

    cookie.cookie_value = 'Hm_lvt_7d206fb19764ee3cea0cf0a719cf78b8=1515211354; Hm_lpvt_7d206fb19764ee3cea0cf0a719cf78b8=1515215017; UM_distinctid=160b14ab3cd64b-088b2225e7be8-e323462-1fa400-160b14ab3ceb8d; visid_incap_1454401=aNtdhD3qSGCI0aMQfB7xJAUPSloAAAAAQUIPAAAAAACClNqa2hhNCPTUqDHIzPgd; incap_ses_199_1454401=C/0yDab7kV2ttpvMEv7CAuJrUFoAAAAAvjKYEdCQWTrmgGaV0iGi9Q==; CNZZDATA1257366169=1353596363-1514802362-null%7C1515218164'
    cookies = cookie.resolve()
    print(cookies)
    data={'cookies':cookies}
    return render_template('home/detail.html',**data)


@home.route('/add_fiction',methods=['GET','POST'])
def add_fiction():
    if request.method == 'POST':
        fiction = Fiction()
        fiction.fiction_name=request.form['fiction_name']
        fiction.fiction_author=request.form['fiction_author']
        if fiction.save():
            data={'errorMsg':'添加成功'}
        else:
            data={'errorMsg':'添加失败'}
        return render_template('home/add_fiction.html',**data)

    return render_template('home/add_fiction.html')